package com.yy.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.domain.Prod;
import com.yy.domain.ProdTagReference;
import com.yy.domain.Sku;
import com.yy.dto.PageDTO;
import com.yy.dto.ProdAddDTO;
import com.yy.dto.ProdQueryDTO;
import com.yy.ex.BusinessException;
import com.yy.mapper.ProdMapper;
import com.yy.mapper.ProdTagReferenceMapper;
import com.yy.mapper.SkuMapper;
import com.yy.service.ProdService;
import com.yy.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
* @author yy5
* @description 针对表【prod(商品表)】的数据库操作Service实现
* @createDate 2023-11-27 20:47:04
*/
@Service
public class ProdServiceImpl extends ServiceImpl<ProdMapper, Prod>
    implements ProdService{

    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private ProdTagReferenceMapper prodTagReferenceMapper;
    /**
     * 分页查询商品
     *
     * @param pageDTO
     * @param prodQueryDTO
     * @return
     */
    @Override
    public Page<Prod> loadProdPage(PageDTO pageDTO, ProdQueryDTO prodQueryDTO) {
        //根据产品名字，状态查询，分商铺
        Page<Prod> prodPage=new Page<>(pageDTO.getCurrent(),pageDTO.getSize());
        Long shopId= AuthUtil.getShopId();
        return this.getBaseMapper().selectPage(prodPage,new LambdaQueryWrapper<Prod>()
                .eq(!shopId.equals(1L),Prod::getShopId,shopId)
                .eq(!ObjectUtils.isEmpty(prodQueryDTO.getStatus()),Prod::getStatus,prodQueryDTO.getStatus())
                .like(StringUtils.hasText(prodQueryDTO.getProdName()),Prod::getProdName,prodQueryDTO.getProdName())
                .orderByDesc(Prod::getUpdateTime)
        );
    }

    /**
     * 新增商品
     *写prod
     *写sku
     *写tag_reference
     * @param prodAddDTO
     * @return
     */
    @Override
    public Integer addProd(ProdAddDTO prodAddDTO) {
        //首先判断该商品是否存在
        Long shopId=AuthUtil.getShopId();
        Long count=this.getBaseMapper().selectCount(new LambdaQueryWrapper<Prod>()
                .eq(Prod::getShopId,shopId)
                .eq(Prod::getProdName,prodAddDTO.getProdName())
        );
        if (count>0){
            throw new BusinessException("该商品已经存在");
        }
        prodAddDTO.setShopId(shopId);
        prodAddDTO.setSoldNum(0);
        prodAddDTO.setCreateTime(new Date());
        prodAddDTO.setUpdateTime(new Date());
        if (prodAddDTO.getStatus().equals(1)) {
            prodAddDTO.setPutawayTime(new Date());
        }
        ProdAddDTO.DeliveryModeVo deliveryModeVo = prodAddDTO.getDeliveryModeVo();
        prodAddDTO.setDeliveryMode(JSON.toJSONString(deliveryModeVo));
        int i = getBaseMapper().insert(prodAddDTO);
        handleSku(prodAddDTO);
        handleTagReference(prodAddDTO);
        return i;
    }



    /**
     * 处理标签关系
     *
     * @param prodAddDTO
     */
    private void handleTagReference(ProdAddDTO prodAddDTO) {
        List<Long> tagList = prodAddDTO.getTagList();
        if (CollectionUtils.isEmpty(tagList)) {
            return;
        }
        Long prodId = prodAddDTO.getProdId();
        tagList.forEach(tid -> {
            ProdTagReference prodTagReference = new ProdTagReference();
            prodTagReference.setTagId(tid);
            prodTagReference.setProdId(prodId);
            prodTagReference.setStatus(1);
            prodTagReference.setCreateTime(new Date());
            prodTagReferenceMapper.insert(prodTagReference);
        });

    }


    /**
     * 处理sku表
     *
     * @param prodAddDTO
     */
    private void handleSku(ProdAddDTO prodAddDTO) {
        List<Sku> skuList = prodAddDTO.getSkuList();
        if (CollectionUtils.isEmpty(skuList)) {
            return;
        }
        Long prodId = prodAddDTO.getProdId();
        skuList.forEach(sku -> {
            sku.setCreateTime(new Date());
            sku.setUpdateTime(new Date());
            sku.setProdId(prodId);
            skuMapper.insert(sku);
        });

    }

}




